package ptumall.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import ptumall.dao.shop.GoodsCategoryDao;
import ptumall.dao.shop.GoodsDao;
import ptumall.model.shop.Category;
import ptumall.model.shop.GoodsItem;
import ptumall.model.PageResult;

import java.util.List;

// MallService.java
@Service
@RequiredArgsConstructor
public class MallService {
    private final GoodsCategoryDao categoryDao;
    private final GoodsDao goodsDao;

    public List<Category> getActiveCategories() {
        return categoryDao.selectActiveCategories();
    }

    public PageResult<GoodsItem> getGoodsByCategory(Long categoryId, Integer page, Integer pageSize) {
        // 参数校验
        if (page < 1) page = 1;
        if (pageSize < 1 || pageSize > 100) pageSize = 10;

        // 计算分页
        int offset = (page - 1) * pageSize;

        // 查询数据
        List<GoodsItem> items = goodsDao.selectByCategoryPage(categoryId, offset, pageSize);
        Long total = goodsDao.countByCategory(categoryId);

        return new PageResult<>(total, items, page, pageSize);
    }
}
